package com.example.dairenkun.neufoodshop.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.dairenkun.neufoodshop.R;
import com.example.dairenkun.neufoodshop.adapter.OrderListAdapter;
import com.example.dairenkun.neufoodshop.bean.AddComment;
import com.example.dairenkun.neufoodshop.bean.Food;
import com.example.dairenkun.neufoodshop.bean.Order;
import com.example.dairenkun.neufoodshop.model.FoodModel;
import com.example.dairenkun.neufoodshop.model.OrderModel;
import com.example.dairenkun.neufoodshop.service.CommentService;
import com.example.dairenkun.neufoodshop.service.FoodService;
import com.example.dairenkun.neufoodshop.service.OrderService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class AddCommentActivity extends AppCompatActivity {
private ImageView imagefood;
    private TextView shopname;
    private  TextView foodname;
    private  TextView price;
    private EditText comment_content;
    private Button btn_add;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_comment);
        initViews();
        getData();

        Intent intent =getIntent();
        shopname.setText(intent.getStringExtra("shopname"));
        foodname.setText(intent.getStringExtra("foodname"));
        price.setText(intent.getStringExtra("price")+"元");
        //
       btn_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent =getIntent();
                final int orderid=Integer.valueOf(intent.getStringExtra("order_id"));
                //获取网上的数据/
              /*  Retrofit retrofit=new Retrofit.Builder()
                        .baseUrl("http://60.205.189.39/ ")
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
                CommentService Service=retrofit.create(CommentService.class);
                Call<AddComment> call=Service.InsertComment(orderid,comment_content.getText().toString());*/
                OrderModel orderModel=new OrderModel();
                Call call= orderModel.AddComment(orderid,comment_content.getText().toString());
                call.enqueue(new Callback<AddComment>() {
                    @Override
                    public void onResponse(Call<AddComment> call, retrofit2.Response<AddComment> response)
                    {
                        AddComment res=response.body();
                        String result=res.getSuccess();

                        if("0".equals(result))
                        {
                            //失败处理
                            Toast.makeText(AddCommentActivity.this, "评论失败！", Toast.LENGTH_SHORT).show();
                        } else
                        {
                            //登录成功处理
                            Toast.makeText(AddCommentActivity.this, "评论成功！", Toast.LENGTH_SHORT).show();


                        }

                    }
                    @Override
                    public void onFailure(Call<AddComment> call, Throwable t) {

                    }
                });
            }
        });


    }
    private void getData() {
        Intent intent =getIntent();
        final int foodid=Integer.valueOf(intent.getStringExtra("food_id"));
        //获取网上的数据/
        FoodModel foodModel=new FoodModel();
        Call call=foodModel.FoodDetail(foodid);
        call.enqueue(new Callback<Food>() {
            @Override
            public void onResponse(Call<Food> call, retrofit2.Response<Food> response)
            {
                Food food=response.body();
            //imagefood.setImageURI();
            }
            @Override
            public void onFailure(Call<Food> call, Throwable t) {

            }
        });
    }

    public  void initViews(){
        imagefood=(ImageView) findViewById(R.id.image_addcomment);
        shopname=(TextView) findViewById(R.id.addcomment_shopname);
        foodname=(TextView)findViewById(R.id.addcomment_foodname);
         price=(TextView)findViewById(R.id.addcomment_price);
         comment_content=(EditText)findViewById(R.id.addcomment_content);
         btn_add=(Button)findViewById(R.id.btn_addcomment);
    }
}

